#!/sbin/openrc-run

NAME=${SVCNAME##*.}
if [ -n "${NAME}" -a "${SVCNAME}" != "automatic" ]; then
    PID="/run/automatic.${NAME}.pid"
    PNAME=$(echo ${RC_SVCNAME} | sed 's/\..*//g')
    CONF_DEFAULT="/etc/conf.d/automatic.${NAME}"
else
    PID="/run/automatic.pid"
    PNAME=${RC_SVCNAME}
    CONF_DEFAULT="/etc/conf.d/automatic"
fi
CONF=${CONF:-${CONF_DEFAULT}}
EXEC=${EXEC:-/usr/bin/automatic}

depend() {
	need net
	provide automatic
}

start() {
	ebegin "Starting automatic"
	start-stop-daemon --start --quiet --make-pidfile --pidfile ${PID} --exec ${EXEC} -- ${OPTS} ${CONFIG}
	eend $?
}

start_post() {
	pgrep -n $(echo ${PNAME}) > ${PID}
}

stop() {
	ebegin "Stopping automatic"
	start-stop-daemon --stop --quiet --pidfile ${PID}
	rm -f ${PID}
	eend $?
}
